package io.geekidea.springbootplus.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.geekidea.springbootplus.system.entity.Carrier;
import io.geekidea.springbootplus.system.entity.Customer;
import io.geekidea.springbootplus.system.entity.Task;
import io.geekidea.springbootplus.system.service.SubscribeMessageService;
import io.geekidea.springbootplus.system.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class SubscribeMessageServiceImpl implements SubscribeMessageService {
    @Override
    public int sendMessage(JSONObject jsonObject) {
        String res = Utils.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5cda879d33846e26&secret=974aa705410555f950d9bdbda1b2d8c8");

        JSONObject o = JSON.parseObject(res);

        String token = o.getString("access_token");

        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+token;

        String r = Utils.post(url,JSON.toJSONString(jsonObject));
        System.out.println(r);
        return 1;
    }

    public void sendMessage(Customer customer, Task task, Carrier carrier,String status){

        String openId = customer.getOpenID();

        String thing = "快递公司："+task.getCourierCompany()+" 取件码: "+task.getPickupCode();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date();

        String currentTimeStr = sdf.format(date);

        String s = "{\n" +
                "  \"touser\": \""+openId+"\",\n" +
                "  \"template_id\": \"8VhR4ruZ-b6kZRzSzQ4TwI-Y8Rh8vSR1kg5R7kFDrfQ\",\n" +
                "  \"page\": \"pages/progress\",\n" +
                "  \"miniprogram_state\":\"developer\",\n" +
                "  \"lang\":\"zh_CN\",\n" +
                "  \"data\": {\n" +
                "      \"thing7\": {\n" +
                "          \"value\": \""+thing+"\"\n" +
                "      },\n" +
                "      \"phrase2\": {\n" +
                "          \"value\": \""+status+"\"\n" +
                "      },\n" +
                "      \"name5\": {\n" +
                "          \"value\": \""+carrier.getPersonName()+"\"\n" +
                "      } ,\n" +
                "      \"phone_number4\": {\n" +
                "          \"value\": \""+carrier.getPhone()+"\"\n" +
                "      },\n" +
                "      \"date9\": {\n" +
                "          \"value\": \""+currentTimeStr+"\"\n" +
                "      }\n" +
                "  }\n" +
                "}\n";

        JSONObject json = (JSONObject) JSON.parse(s);
        if(status.equals("网点已提件")){
            s = "{\n" +
                    "  \"touser\": \""+openId+"\",\n" +
                    "  \"template_id\": \"CGlLzkCZy2Q5XaMvRw1-9gfHy_CRhk4_6LuXhKK2vI4\",\n" +
                    "  \"page\": \"pages/progress\",\n" +
                    "  \"miniprogram_state\":\"developer\",\n" +
                    "  \"lang\":\"zh_CN\",\n" +
                    "  \"data\": {\n" +
                    "      \"number1\": {\n" +
                    "          \"value\": \""+task.getPickupCode()+"\"\n" +
                    "      },\n" +
                    "      \"phrase2\": {\n" +
                    "          \"value\": \""+task.getCourierCompany()+"\"\n" +
                    "      },\n" +
                    "      \"name8\": {\n" +
                    "          \"value\": \""+carrier.getPersonName()+"\"\n" +
                    "      } ,\n" +
                    "      \"phone_number5\": {\n" +
                    "          \"value\": \""+carrier.getPhone()+"\"\n" +
                    "      },\n" +
                    "      \"date10\": {\n" +
                    "          \"value\": \""+currentTimeStr+"\"\n" +
                    "      }\n" +
                    "  }\n" +
                    "}\n";
            json = (JSONObject) JSON.parse(s);
        }else if(status.equals("已送至楼下")){
            s = "{\n" +
                    "  \"touser\": \""+openId+"\",\n" +
                    "  \"template_id\": \"Lr1MlntU2DKsuxtrlTJT_d3uxOeIvKjIsjzHSKUpUOI\",\n" +
                    "  \"page\": \"pages/progress\",\n" +
                    "  \"miniprogram_state\":\"developer\",\n" +
                    "  \"lang\":\"zh_CN\",\n" +
                    "  \"data\": {\n" +
                    "      \"thing18\": {\n" +
                    "          \"value\": \""+thing+"\"\n" +
                    "      },\n" +
                    "      \"thing22\": {\n" +
                    "          \"value\": \""+carrier.getPersonName()+"\"\n" +
                    "      },\n" +
                    "      \"phrase3\": {\n" +
                    "          \"value\": \""+status+"\"\n" +
                    "      } ,\n" +
                    "      \"time2\": {\n" +
                    "          \"value\": \""+currentTimeStr+"\"\n" +
                    "      }\n" +
                    "  }\n" +
                    "}\n";
            json = (JSONObject) JSON.parse(s);
        }else{
            json.put("template_id","8VhR4ruZ-b6kZRzSzQ4TwIoQKtz7ODjkkHCgAsalFSE");
        }
        sendMessage(json);

    }


}
